package sys.Flight_Information_Sysytem.dao.impl;

import sys.Flight_Information_Sysytem.bean.AirInfo;
import sys.Flight_Information_Sysytem.dao.AirInfoDao;
import sys.Message_Sysytem.utils.DBUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AirInfoDaoImpl extends DBUtils implements AirInfoDao {


    @Override
    public List<AirInfo> findAll() {
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo";
            resultSet = query(sql,null);
            while (resultSet.next()){
                AirInfo airInfo = new AirInfo();
                airInfo.setNumber(resultSet.getString("number"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setBeginTime(resultSet.getDate("begintime"));
                arrayList.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return arrayList;
    }

    @Override
    public List<AirInfo> findByTime(String time) {
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo where begintime=?";
            ArrayList params = new ArrayList();
            params.add(time);
            resultSet = query(sql,params);
            while (resultSet.next()){
                AirInfo airInfo = new AirInfo();
                airInfo.setNumber(resultSet.getString("number"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setBeginTime(resultSet.getDate("begintime"));
                arrayList.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return arrayList;
    }
    @Override
    public List<AirInfo> findByAddress(String address) {
        ArrayList arrayList = new ArrayList();
        try {
            String sql = "select * from airinfo where address like ?";
            ArrayList params = new ArrayList();
            params.add("%"+address+"%");
            resultSet = query(sql,params);
            while (resultSet.next()){
                AirInfo airInfo = new AirInfo();
                airInfo.setNumber(resultSet.getString("number"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setBeginTime(resultSet.getDate("begintime"));
                arrayList.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return arrayList;
    }

    @Override
    public int delete(String number) {
        int update = 0;
        try {
            ArrayList arrayList = new ArrayList();
            String sql = "delete from airinfo where number=?";
            ArrayList params = new ArrayList();
            params.add(number);
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }


    @Override
    public int update(AirInfo airInfo,String oldNumber,String newBeginTime) {
        int update = 0;
        try {
            String sql = "update airinfo set number =?,address=?,begintime =?  where number=?";
            ArrayList params = new ArrayList();
            params.add(airInfo.getNumber());
            params.add(airInfo.getAddress());
            params.add(newBeginTime);
            params.add(oldNumber);
            update = update(sql,params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public AirInfo findByNumber(String number) {
        AirInfo airInfo = null;
        try {
            String sql = "select * from airinfo where number=?";
            ArrayList params = new ArrayList();
            params.add(number);
            resultSet = query(sql, params);
            while (resultSet.next()){
                airInfo =new AirInfo();
                airInfo.setNumber(resultSet.getString("number"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setBeginTime(resultSet.getDate("begintime"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfo;
    }
}
